package com.pxene.dmp.job.task;

import java.io.BufferedWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.pxene.dmp.common.DateUtils;
import com.pxene.dmp.common.KVRequester;
import com.pxene.dmp.common.KVUrlGenerator;
import com.pxene.dmp.constant.BaseConstant;
import com.pxene.dmp.bean.SearchWordFIX;
import com.pxene.dmp.bean.SearchWordFIX.Content;

public class SearchWordFIXTask implements Runnable {

	private final Map<String, String> params;
	private final BufferedWriter writer;
	private final CountDownLatch latch;

	private Log log = LogFactory.getLog(SearchWordFIXTask.class);
	private BufferedWriter bw;

	public SearchWordFIXTask(String index, String date, BufferedWriter writer, CountDownLatch latch,
			BufferedWriter bw) {
		this.params = new HashMap<String, String>();
		this.params.put("index", index);
		this.params.put("date", date);
		this.writer = writer;
		this.latch = latch;
		this.bw = bw;
	}

	@Override
	public void run() {
		String url = KVUrlGenerator.getCustUrl(BaseConstant.API_GET_SEARCHWORDFIX);
		KVRequester reqer = new KVRequester(url, params, bw);
		SearchWordFIX resp = (SearchWordFIX) reqer.execute(SearchWordFIX.class);
		if (resp != null) {
			if (resp.getCode() == 200) {
				try {
					Content data = resp.getData();
					writer.write(data.getAccount() + "|" + parseChn(data.getWords()) + "|" + data.getSource() + "|"
							+ data.getTime() + "|" + data.getProvince() + "\n");
					bw.write("[SUCCESS] " + DateUtils.getNowFormat("yyyy-MM-dd HH:mm:ss") + " ["
							+ Thread.currentThread().getName() + "] [" + SearchWordFIXTask.class.getName() + "] "
							+ params + "\n");
				} catch (Exception e) {
					log.error(BaseConstant.LOG_PREFIX + params + "Write File Exception", e);
					try {
						bw.write("[ERROR] " + DateUtils.getNowFormat("yyyy-MM-dd HH:mm:ss") + " ["
								+ Thread.currentThread().getName() + "] [" + SearchWordFIXTask.class.getName() + "] "
								+ "<=ee-debug=>" + params + "Write File Exception:" + e + "\n");
					} catch (Exception e1) {
						e1.printStackTrace();
					}
				}
			} else {
				log.error(BaseConstant.LOG_PREFIX + params + "request fail:" + resp.getMessage());
				try {
					bw.write("[ERROR] " + DateUtils.getNowFormat("yyyy-MM-dd HH:mm:ss") + " ["
							+ Thread.currentThread().getName() + "] [" + SearchWordFIXTask.class.getName() + "] "
							+ "<=ee-debug=>" + this.params + "request fail:" + resp.getMessage() + "\n");
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		}
		reqer.close();
		latch.countDown();
	}

	private static  String parseChn(String chn) {
		// 双竖线分割不同的搜素
		String[] searchs = chn.split("\\|");
		for (int i = 0; i < searchs.length; i++) {
			// 删除k-|
			searchs[i] = searchs[i].replace("k-", "").replaceAll("\\|", "");
			if (searchs[i].contains("#")) {
				StringBuilder sb = new StringBuilder();
				int start = 0;
				int end = -1;
				while ((end = searchs[i].indexOf("#", start)) != -1) {
					sb.append(searchs[i].substring(start, end))
							.append((char) Integer.parseInt(searchs[i].substring(end + 1, end + 5), 16));
					start = end + 5;
				}
				searchs[i] = sb.toString();
			}
		}
		return StringUtils.join(searchs, "^");
	}
	
	public static void main(String[] args) {
		System.out.println(parseChn("#9648#656c#8f89fadsfdasf234"));
	}
}
